home *** CD-ROM | disk | FTP | other *** search
/ MacFormat 1995 March / macformat-022.iso / Shareware City / Developers / src / sfget-sfput-nshell.hqx / sf-cmds / source / sfput.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-10-10  |  5.9 KB  |  306 lines

  1. /* ========== the commmand file: ==========
  2.  
  3.     sfput.c
  4.     
  5.     Copyright (c) 1994 Newport Software Development
  6.     
  7.     You may distribute unmodified copies of this file for
  8.     noncommercial purposes.  You may use this file as a
  9.     reference when writing your own nShell(tm) commands.
  10.     
  11.     All other rights are reserved.
  12.     
  13.    ========== the commmand file: ========== */
  14.  
  15. #ifdef __MWERKS__            // Get the appropriate A4 stuff
  16. #include <A4Stuff.h>
  17. #else
  18. #include <SetUpA4.h>
  19. #endif
  20.  
  21. #include <GestaltEqu.h>
  22. #include <Folders.h>
  23.  
  24. #include "nshc.h"
  25. #include "str_utl.proto.h"
  26. #include "nshc_utl.proto.h"
  27.  
  28. /* ======================================== */
  29.  
  30. // globals
  31.  
  32. Str32    gVarName;
  33. Str255    gPath;
  34. short    gFileRef;
  35. Str255    gPrompt;
  36.  
  37. /* ======================================== */
  38.  
  39. // prototypes - for local use only
  40.  
  41. int     sfput_open_rsrc(t_nshc_parms *nshc_parms, t_nshc_calls *nshc_calls);
  42. void    sfput_close_rsrc(void);
  43. int     sfput( void );
  44. int     sfput_init(t_nshc_parms *nshc_parms, t_nshc_calls *nshc_calls );
  45. int     sfput_path( StandardFileReply *sfReply );
  46. int     sfput_pre7( void );
  47.  
  48. /* ======================================== */
  49.  
  50. // utility
  51.  
  52. int    sfput_pre7( void )
  53. {
  54.     long    response;
  55.     
  56.     if ( Gestalt( 'sysv', &response ) )
  57.         return( 1 );
  58.         
  59.     if ( response < 0x700 )
  60.         return( 1 );
  61.     else
  62.         return( 0 );
  63. }
  64.  
  65. int sfput_path( StandardFileReply *sfReply )
  66. {
  67.     CInfoPBRec    block;
  68.     int            error;
  69.     Str255        temp;
  70.  
  71.     error = 0;
  72.     gPath[0] = 0;
  73.     temp[0] = 0;
  74.     block.dirInfo.ioNamePtr = temp;
  75.     block.dirInfo.ioDrParID = sfReply->sfFile.parID;
  76.     
  77.     if ( sfReply->sfFile.parID != 1)
  78.         do {
  79.         
  80.             block.dirInfo.ioVRefNum = sfReply->sfFile.vRefNum;
  81.             block.dirInfo.ioFDirIndex = -1;
  82.             block.dirInfo.ioDrDirID = block.dirInfo.ioDrParID;
  83.     
  84.             error = PBGetCatInfo(&block,false);
  85.             
  86.             if ( temp[0] + gPath[0] + 1 > 255 )
  87.                 error = 1;
  88.             
  89.             if (!error) {
  90.                 temp[ ++temp[0] ] = ':';
  91.                 pStrAppend(temp,gPath);
  92.                 pStrCopy(gPath,temp);
  93.                 }
  94.     
  95.         } while (!error && (block.dirInfo.ioDrDirID != fsRtDirID));
  96.  
  97.     if ( error )
  98.         return( error );
  99.  
  100.     if ( gPath[0] + sfReply->sfFile.name[0] > 255 )
  101.         error = 1;
  102.     else
  103.         pStrAppend( gPath, sfReply->sfFile.name );
  104.  
  105.     if ( sfReply->sfFile.parID == 1 )
  106.         if ( gPath[0] < 255 )
  107.             gPath[ ++gPath[0] ] = ':';
  108.         else
  109.             error = 1;
  110.  
  111.     return( error );
  112. }
  113.  
  114. /* ======================================== */
  115.  
  116. // sfput
  117.  
  118. int sfput( void )
  119. {
  120.     StandardFileReply    sfReply;
  121.     int                    my_err;
  122.  
  123.     StandardPutFile(gPrompt,gPath,&sfReply);
  124.     
  125.     if (sfReply.sfGood)
  126.         my_err = sfput_path( &sfReply );
  127.     else
  128.         my_err = 1;
  129.         
  130.     return( my_err );
  131. }
  132.  
  133. /* ======================================== */
  134.  
  135. int sfput_init(t_nshc_parms *nshc_parms, t_nshc_calls *nshc_calls )
  136. {
  137.     int        usage;
  138.     int        prompt;
  139.     int        var;
  140.     int        argc;
  141.     int        error;
  142.     int        i;
  143.     char    c;
  144.     char    *p;
  145.     char    *q;
  146.     
  147.     // return if bad include file version
  148.         
  149.     if (nshc_bad_version( nshc_parms, nshc_calls, NSHC_VERSION ))
  150.         return( 0 );
  151.     
  152.     // return if pre system 7 os
  153.         
  154.     if ( sfput_pre7() ) {
  155.         nshc_calls->NSH_putStr_err("\psfput: This command requires System 7.\r");
  156.         nshc_parms->result = NSHC_ERR_GENERAL;
  157.         nshc_parms->action = nsh_idle;
  158.         return( 0 );
  159.         }
  160.  
  161.     // return if bad usage
  162.         
  163.     usage = 0;
  164.     argc = nshc_parms->argc;
  165.     prompt = nshc_got_option(nshc_parms, 'p');
  166.  
  167.     if ((argc != 2) && (argc != 4))
  168.         usage = 1;
  169.         
  170.     if ( (argc == 4) && ( prompt != 1 ) && ( prompt != 2 ) )
  171.         usage = 1;
  172.     
  173.     if ( usage ) {
  174.         nshc_calls->NSH_putStr_err("\pUsage: sfput variable_name [-p \"prompt string\"].\r");
  175.         nshc_parms->result = NSHC_ERR_PARMS;
  176.         nshc_parms->action = nsh_idle;
  177.         return( 0 );
  178.         }
  179.         
  180.     // return if bad prompt string
  181.     
  182.     if (prompt) {
  183.     
  184.         p = &nshc_parms->arg_buf[nshc_parms->argv[prompt + 1]];
  185.             
  186.         if ( cStrLen( p ) > 255 ) {
  187.             nshc_calls->NSH_putStr_err("\psfput: Prompt string is too long.");
  188.             nshc_parms->result = NSHC_ERR_PARMS;
  189.             nshc_parms->action = nsh_idle;
  190.             return( 0 );
  191.             }
  192.         else
  193.             pStrFromC( gPrompt, p );
  194.             
  195.         }
  196.     else
  197.         pStrCopy( gPrompt, "\pSave as:");
  198.     
  199.     // return if bad variable name
  200.     
  201.     if ( prompt == 1 )
  202.         var = 3;
  203.     else
  204.         var = 1;
  205.         
  206.     p = q = &nshc_parms->arg_buf[ nshc_parms->argv[ var ] ];
  207.     i = 0;
  208.     while (c = *p++) {
  209.         error = 1;
  210.         if ( c == '_' ) error = 0; else
  211.         if ( ( c >= 'a' ) && ( c <= 'z' ) ) error = 0; else
  212.         if ( ( c >= 'A' ) && ( c <= 'Z' ) ) error = 0; else
  213.         if ( ( c >= '0' ) && ( c <= '9' ) ) error = 0;
  214.         if (error) {
  215.             nshc_calls->NSH_putStr_err( "\psfput: Invalid variable name = " );
  216.             nshc_calls->NSH_puts_err( q );
  217.             nshc_calls->NSH_putchar_err( '\r' );
  218.             nshc_parms->result = NSHC_ERR_PARMS;
  219.             nshc_parms->action = nsh_idle;
  220.             return( 0 );
  221.             }
  222.         gVarName[++i] = c;
  223.         }
  224.         
  225.     if ( i > 31 ) {
  226.         nshc_calls->NSH_putStr_err( "\psfput: Variable name too long = " );
  227.         nshc_calls->NSH_puts_err( q );
  228.         nshc_calls->NSH_putchar_err( '\r' );
  229.         nshc_parms->result = NSHC_ERR_PARMS;
  230.         nshc_parms->action = nsh_idle;
  231.         return( 0 );
  232.         }
  233.     else
  234.         gVarName[0] = i;
  235.         
  236.     gPath[0] = 0;
  237.     
  238.     return( 1 );
  239. }
  240.  
  241. /* ======================================== */
  242.  
  243. int sfput_open_rsrc(t_nshc_parms *nshc_parms, t_nshc_calls *nshc_calls)
  244. {
  245.     Str255    fileName;
  246.  
  247.     pStrFromC( fileName, &nshc_parms->arg_buf[nshc_parms->argv[0]] );
  248.     
  249.     gFileRef = -1;
  250.     
  251.     if ( !nshc_calls->NSH_path_which( fileName ) )
  252.         gFileRef = OpenResFile( fileName );
  253.         
  254.     if ( gFileRef < 0 ) {
  255.         nshc_calls->NSH_putStr_err("\psfput: Could not open resource file.\r");
  256.         nshc_parms->result = NSHC_ERR_PARMS;
  257.         nshc_parms->action = nsh_idle;
  258.         return( 1 );
  259.         }
  260.     else
  261.         return( 0 );
  262. }
  263.  
  264. void sfput_close_rsrc(void)
  265. {
  266.     if (gFileRef)
  267.         CloseResFile(gFileRef);
  268. }
  269.  
  270. /* ======================================== */
  271.  
  272. void main(t_nshc_parms *nshc_parms, t_nshc_calls *nshc_calls)
  273. {
  274.     int        error;
  275.     
  276. #ifdef __MWERKS__
  277.     long oldA4  = SetCurrentA4();
  278. #else
  279.     RememberA0();
  280.     SetUpA4();
  281. #endif
  282.     
  283.     if ( sfput_init( nshc_parms, nshc_calls ) ) {
  284.     
  285.         error = sfput_open_rsrc( nshc_parms, nshc_calls );
  286.     
  287.         if ( !error )
  288.             error = sfput();
  289.             
  290.         sfput_close_rsrc();
  291.         
  292.         if ( !error )
  293.             nshc_calls->NSH_var_set( gVarName, gPath );
  294.  
  295.          nshc_parms->action = nsh_idle;
  296.         nshc_parms->result = error;
  297.             
  298.         }
  299.  
  300. #ifdef __MWERKS__
  301.     SetA4(oldA4);
  302. #else
  303.     RestoreA4();
  304. #endif
  305. }
  306.